package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.BussinessDao;
import com.example.demo.dao.WorkerDao;
import com.example.demo.pojo.Business;
import com.example.demo.pojo.Message;
import com.example.demo.pojo.Worker;
import com.example.demo.service.GetPassService;
import com.example.demo.utl.ImageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

@Service
public class GetPassServiceImpl implements GetPassService {
    @Resource
    ImageUtil imageUtil;
    @Resource
    BussinessDao bussinessDao;
    @Resource
    WorkerDao workerDao;
    @Override
    public void getPassCode(HttpSession session , HttpServletResponse resp) {

        Map<String , BufferedImage> imageMap = imageUtil.createImage();
        String code = imageMap.keySet().iterator().next();
        session.setAttribute("code", code);

        BufferedImage image = imageMap.get(code);
        try {
            ServletOutputStream out = resp.getOutputStream();
            ImageIO.write(image, "jpg", out);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public String getPass(HttpSession session ,String account ,String option,String accountanser,String accountcode) {
        String code = (String) session.getAttribute("code");

        accountcode = accountcode.replaceAll(" ","");
        account = account.replaceAll(" ","");
        accountanser = accountanser.replaceAll(" ","");


        Message message = new Message();
        if(account!=null && accountanser!=null && accountcode!=null) {
            if (code.equals(accountcode)) {
                int len = account.length();
                if (len == 11) {
                    account = account.replaceAll("0", "");
                    String count = account.substring(0, 2);
                    String seq = account.substring(2);

                    session.setAttribute("count",count);
                    session.setAttribute("seq",seq);

                    if (count.equals("BU")) {
                        Business business = bussinessDao.businessselect(count, seq);
                        if (business != null) {
                            if (business.getBuQuestion().equals(option) && business.getBuAnswer().equals(accountanser)) {
                                message.setInf("成功");
                            } else {
                                message.setInf("密保错误");
                            }
                        } else {
                            message.setInf("账户错误");
                        }
                    } else if (count.equals("GR")) {
                        Worker worker = workerDao.workerSelect(count, seq);
                        if (worker != null) {
                            if (worker.getGrQuestion().equals(option) && worker.getGrAnswer().equals(accountanser)) {
                                message.setInf("成功");
                            } else {
                                message.setInf("密保错误");
                            }
                        } else {
                            message.setInf("账户错误");
                        }
                    }
                } else {
                    message.setInf("账户错误");
                }
            } else {
                message.setInf("验证码不正确");
            }

        }else{
            message.setInf("数据不能为空");
        }
        String js = JSONObject.toJSONString(message);
        return js;
    }

    @Override
    public String changePass(HttpSession session, String accountpass) {
        String count = (String) session.getAttribute("count");
        String seq = (String) session.getAttribute("seq");

        Message message =new Message();
        if(count.equals("BU")){

            int re = bussinessDao.changePass(count,seq,accountpass);
            if(re>0){
                message.setInf("修改成功");
            }else{
                message.setInf("修改失败");
            }

        }else if(count.equals("GR")){
            int re = workerDao.changePass(count,seq,accountpass);
            if(re>0){
                message.setInf("修改成功");
            }else{
                message.setInf("修改失败");
            }
        }else{
            message.setInf("修改失败");
        }
        String js = JSONObject.toJSONString(message);
        return js;
    }
}
